function extract_response_config () {

    const items = $('.response .tree-view-wrapper > div > div > div:last-child > div')

    const config = []

    $.each(items, (index, item) => {
        extract_response_item_config(item, config)
    })

    return config

}

function extract_response_item_config (item, config) {

    // 是否是叶子节点
    let isLeaf = true

    const obj = $(item)

    if (obj.find('.tree-view-item-node').length > 0) {
        isLeaf = false
    }

    if (isLeaf) {
        const detail = obj.find('.tree-view-item-leaf > span')

        config.push({
            name: detail[0].innerText.replaceAll("\n","").replaceAll(" ","").replaceAll('"',"").replace(":",""),
            type: detail[1].innerText,
            description: detail[3].innerText,
            // required: detail[2].innerText === "true"
        })
    } else {
        const list = obj.find('.tree-view-item-leaf > div')

        const detail = $(list[0]).find('span')

        const child_config = []
        config.push({
            name: detail[0].innerText.replaceAll("\n","").replaceAll(" ","").replaceAll('"',"").replace(":",""),
            type: 'model',
            description: detail[1].innerText,
            child: child_config
        })

        const child_items = $(list[1]).find('> div')
        $.each(child_items, (index, child_item) => {
            extract_response_item_config(child_item, child_config)
            
        })

    }

}





function extract_config () {

    console.info('extract config')

    const config = {}

    config.name = $('h1').text()
    config.method = $('.method-area').text()
    config.url = $('.url').text()
    config.gitee_url = document.location.href


    const parameter_config = []

    $.each($('.ivu-form').find('tr'),(i,n)=>{
        const config_item = $(n).find('td')
        if (config_item && config_item.length === 5) {

            const name = config_item[0].innerText

            parameter_config.push({

                name: name.replace('*', ''),
                description: config_item[2].innerText,
                type: config_item[3].innerText,
                data_type: config_item[4].innerText,
                required: name.indexOf('*') >= 0

            })

        }

    })
    config.parameter = parameter_config


    const response_config = extract_response_config()

    config.response = response_config

    console.info(config)
    // show_msg(JSON.stringify(config, null, 2))
    return config

}

let editor = null

function show_msg (msg) {

    // $('#plugin-result').html(msg)
    // hljs.highlightAll();
    editor.setValue(msg)

}

function show_msg_in_dialog (msg) {

    if ($('.plugin_msg_area').length === 0) {
        $("body").append('<div class="msg_area plugin_msg_area" id="plugin_msg_area"><pre><code class="language-json" id="msg_textarea" class="msg_textarea"></code></pre><div>')
    }

    $("#msg_textarea").html(msg)

    $('#plugin_msg_area').dialog({
        height: 600,
        width: 800,
        closeOnEscape: true
    })
    

}

function init_page () {

    setTimeout(() => {
        $('.obody').parent().append('<div class="plugin-card"><textarea id="plugin-result" class="plugin-result"></textarea></div>')
        const config = extract_config()

        editor = CodeMirror.fromTextArea(document.getElementById("plugin-result"),{
            //Js高亮显示
            mode:"application/json",
            indentUnit : 2,  // 缩进单位，默认2
            smartIndent : true,  // 是否智能缩进
            //显示行号
            styleActiveLine: true,
            lineNumbers:true,
            lineWrapping:true,
            theme: "default",
            // //设置主题
            // theme:"eclipse",
            // //绑定Vim
            // keyMap:"vim",
    
            //代码折叠
            lineWrapping:true,
            foldGutter: true,
            gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
            foldOptions: {
                rangeFinder: CodeMirror.fold.indent,
                indentUnit: 4
            },
            matchBrackets: true,
            styleActiveLine: true,
            
        })
        show_msg(JSON.stringify(config, null, 4))


    }, 200)

    // $("body").append('<div id="extract_config_btn" class="api_menu">提取Api</div>')

    // $("#extract_config_btn").click(() => {
    //     const config = extract_config()
    //         show_msg_in_dialog(JSON.stringify(config, null, 4))
    // })

    $('.olist a').click(() => {
        console.info('link click')
        setTimeout(() => {
            const config = extract_config()
            show_msg(JSON.stringify(config, null, 4))
        }, 100)
    })
}
$(document).ready(() => {

    console.info('Gitee Api chrome plugin started!')
    init_page()
    
})